<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/8/25
 * Time: 1:17
 * QQ:1467572213
 */

namespace app\admin\controller;

use library\Controller;
use think\facade\Request;
use think\Db;
use think\facade\Session;
use \app\admin\model\Recharge as Recharge_m;
use \app\index\model\Notify as Notify_m;
use \app\index\model\Base;
/**
 * 充值管理
 * Class Withdrawal
 * @package app\admin\controller
 */

class Recharge extends Controller
{
    /**
     * 充值列表
     * @auth true
     * @menu true
     */
    public function index()
    {
        date_default_timezone_set('America/Mexico_City');
        $this->title = '充值管理';
        $keyword = input('get.keyword/s','');
        $p_order_id = input('get.p_order_id/s','');
        $create_time = input('get.create_time/s','');
        $status = input('get.status/s','');
        
        if ($keyword){
            $sum = Recharge_m::sum($keyword);
            $this->assign('sum',$sum);
        }
        $list = Recharge_m::index($keyword,$p_order_id,$create_time,$status);
        $page = $list->render();  //构造分页
        $this->assign('pages', $page);   //输出分页
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 充值接口
     * @auth true
     * @menu true
     */
    public function recharge_api()
    {
        $this->title = '充值接口';
        $list = Recharge_m::recharge_mode();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 添加接口
     * @auth true
     * @menu true
     */
    public function recharge_add()
    {
        if (Request::isPost()){
            $info['name'] = Request::post('name');
            $info['status'] = Request::post('status');
            $info['create_time'] = time();
            if ($info['name'] && $info['status']){
                $add = Recharge_m::recharge_add($info);
                switch ($add)
                {
                    case 'success':
                        $this->success('添加成功');
                        break;
                    case 'error':
                        $this->error('添加失败');
                        break;
                }
            }else{
                $this->error('请填写完整参数');
            }
        }
        return $this->fetch();
    }

    /**
     * 接口启用
     * @auth true
     * @menu true
     */
    public function recharge_start()
    {
        if (Request::isPost()){
            $status = 1;
            $id = Request::post('id');
            $start = Recharge_m::recharge_status($id,$status);
            switch ($start)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'启用成功'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'启用失败'];
                    break;
            }
        }
    }

    /**
     * 接口禁用
     * @auth true
     * @menu true
     */
    public function recharge_stop()
    {
        if (Request::isPost()){
            $status = 2;
            $id = Request::post('id');
            $start = Recharge_m::recharge_status($id,$status);
            switch ($start)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'停用成功'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'停用失败'];
                    break;
            }
        }
    }
    
    public function topup(){
        $id = input('id');
        $user_info = Db::name('fd_user')->find($id);
        $money = input('money');
        $info = [];
        $info['order_id'] = time().mt_rand(11111,99999); //订单号
        $info['uid'] = $user_info['id'];
        $info['pid'] = $user_info['pid'];
        $info['username'] = $user_info['username'];
        $info['phone'] = $user_info['phone'];
        $info['money'] = $money;
        $info['service_charge'] = $money * Base::get_config('service_cz') / 100;
        $info['type'] = 1;
        $info['status'] = 0;
        $info['the'] = input('img');
        $info['create_time'] = time();
        $info['address'] = input('address');
        $add = Db::name('fd_recharge')->insert($info);
        
        return ['code'=>1,'msg'=>'ok'];
    }
    
    public function determine(){
        $order_id = Db::name('fd_recharge')->find(input('id'));
        Notify_m::shineupay($order_id['order_id']);
        
        return ['code'=>1,'msg'=>'充值成功'];
    }
    
    
    /**
     * 充值列表
     * @auth true
     * @menu true
     */
    public function log()
    {
        $this->title = '充值管理';
        $keyword = input('get.keyword/s','');
        if ($keyword){
            $sum = Recharge_m::sum($keyword);
            $this->assign('sum',$sum);
        }
        $list = Recharge_m::indexs($keyword);
        $page = $list->render();  //构造分页
        $this->assign('pages', $page);   //输出分页
        $this->assign('list',$list);
        return $this->fetch();
    }
    

}